package YourSpace::Controller::Users;
# $WCREV WVDATE$
use strict;
use warnings;
use base 'Catalyst::Controller';

=head1 NAME

YourSpace::Controller::Users - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index 

=cut

sub index : Private {
    my ( $self, $c ) = @_;
    
    if ( $c->user_exists ) {
        
        $c->res->redirect(
            $c->uri_for( '/users/view/' . $c->user->username )
        );
        
    } else {
        
        $c->res->redirect(
            $c->uri_for( '/login', 
                {
                    status_msg => "Login or create a profile!",
                }
            )
        );
    }
}

=head2 list
    
    Fetch all user objects and pass to users/list.tt2 in stash to be displayed
    
=cut
     
sub list : Local {
    # Retrieve the usual perl OO '$self' for this object. $c is the Catalyst
    # 'Context' that's used to 'glue together' the various components
    # that make up the application
    my ($self, $c) = @_;
    
    # Retrieve all of the user records as book model objects and store in the
    # stash where they can be accessed by the TT template
    $c->stash->{users} = [$c->model('YourSpaceDB::User')->all];
        
    # Set the TT template to use.  You will almost always want to do this
    # in your action methods (action methods respond to user input in
    # your controllers).
    $c->stash->{template} = 'users/list.tt2';
}

sub create : Local {
    my ($self, $c) = @_;
    
    # Set the TT template to use
    if ( $c->user_exists ) {
        $c->flash->{error_msg}  = "You are already registered!";
        $c->res->redirect( $c->uri_for("/" ) );
    } else {
        $c->stash->{template} = 'users/create.tt2';
    }
}

sub do_create : Local {

    use Digest::SHA1  qw(sha1_hex);
    my ($self, $c) = @_;

    # Retrieve the values from the form
    my $username    = $c->req->params('username')    || 'N/A';
    my $password    = $c->req->params('passwd')      || 'N/A';
    my $email       = $c->req->params('email')       || 'N/A';
    
    my $enc_passwd  = sha1_hex($password); ## encrypt it
    my $user = $c->model('YourSpaceDB::User')->create({
        username   => $username,
        password   => $enc_passwd,
        email      => $email
    }) or die $!;
    #$book->add_to_users({author_id => $author_id});

    # Store new model object in stash
    $c->stash->{user} = $user;

    # Set the TT template to use
    $c->stash->{template} = 'users/create_done.tt2';
}

sub delete : Local {

    # $id = primary key of book to delete
    my ($self, $c, $id) = @_;
    
    # make sure user has permission to delete users
    # if authorized, delete user, otherwise send 'em back to the main page
    $c->res->redirect(
        $c->uri_for('/', 
            {
                error_msg => "You don't have permission to delete users!" 
            }
        )
    ) unless $c->check_user_roles('admin');
    # Search for the user and then delete it
    $c->model('YourSpaceDB::User')->search({user_id => $id})->delete_all;
    
    # Redirect the user back to the list page with status msg as an arg
    $c->res->redirect($c->uri_for('/users/list', 
        {status_msg => "User deleted."}));

}

sub view : Local {
    # Retrieve the usual perl OO '$self' for this object. $c is the Catalyst
    # 'Context' that's used to 'glue together' the various components
    # that make up the application
    my ($self, $c, $username) = @_;
    
    # Retrieve all of the user records as book model objects and store in the
    # stash where they can be accessed by the TT template
    $c->stash->{user} = $c->model('YourSpaceDB::User')->find( { username => $username } );
        
    # Set the TT template to use.  You will almost always want to do this
    # in your action methods (action methods respond to user input in
    # your controllers).
    $c->stash->{template} = 'users/view.tt2';
}

sub edit : Local {
    my ($self, $c, $username) = @_;

    if ( !$c->user_exists ) {
        $c->flash->{error_msg} = "You aren't logged in";
        return;
    } elsif  (!( $username eq $c->user->username or $c->check_user_roles('admin') )) {
            $c->flash->{error_msg} = "You can not edit this user's profile.";
            $c->res->redirect( $c->uri_for('/users/list/') );
        } else {
            return;
        }
    my $user = $c->model('YourSpaceDB::User')->search( { username => $c->user->username } );
    $c->stash->{user_info}  = $user;
    $c->stash->{template}   = 'users/edit.tt2';
}

sub do_edit : Local {
    my ($self, $c, $username) = @_;
	my $user;
    if ( $c->validate_captcha( $c->req->param( 'validate' ) ) ) {

        # create user object, find them and update their records
       $user = $c->model('YourSpaceDB::User')->search( { username => $username } )->update( 
            { 
                real_name => $c->req->param('realname') 
            }, 
          
        );
        #$user->location ( $c->req->params->{location} );
        #$user->about    ( $c->req->params->{about} );
        #$user->email    ( $c->req->params->{email} );


    } else {

        # CAPTCHA was invalid, send them back to their page
        $c->stash->{template} = 'users/edit.tt2';
        $c->stash->{error_msg} = "Invalid CAPTCHA, please try again";
        return;

    }
    $c->stash->{user_stuff} = $user;
    $c->stash->{status_msg} = "Profile updated!";
    $c->stash->{template} = 'users/edit.tt2';

}
sub moderate : Local {
    my ($self, $c) = @_;
    
    if ( $c->check_user_roles('admin') ) {
        $c->response->body("Welcome, " . $c->user->username);
    } else {
        $c->response->body("Not authorized to be here!");
    }
}

sub online : Local {
    my ($self, $c) = @_;
    $c->response->body("Nothing yet...");
}

=head1 AUTHOR

Devin Austin (timorperfectus.com)

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
